#Copyright Tyler Darnell

#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.

import random
import resourceClasses


class GenericBusiness:
    #defines properties of a generic business
    money = 0
    inputRes = 0 #stock of input
    outputRes = 0 #stock of output
    equipQuality = 0 # 1 - 100
    matUse = 0 #material use per tick
    produce = 0 #amount produced per tick
    minSalePrice = 0 #minimum sale cost for output
    resourceProduced = resourceClasses.Resc("tmp",-1) #resource produced by business
    potentialValue = 0

    def __init__(self, startMon, qual, resource):
        self.money = startMon
        self.equipQuality = qual
        self.minSalePrice = resource.price
        self.resourceProduced = resource

    def produceGoods(self):
        #determine the production for current tick
        self.determineProduction()
        #produce a tick
        self.inputRes = self.inputRes - self.matUse
        self.outputRes = self.outputRes + self.produce

        breakChance = random.randrange(0,9)
        if breakChance >= 7:
            self.equipQuality = self.equipQuality - 5
        self.potentialValue = self.minSalePrice * self.outputRes

    def determineProduction(self):
        #determines production as a function of equipQuality
        #probably will be modified for balance per specific business
        self.matUse = self.equipQuality / 2
        self.produce = self.equipQuality * 2
        print self.produce

    def calcMinSalePrice(self):
        #calculate minimum sale price
        #uses volume in stock
        #sell in bulk = lower price
        if self.outputRes > 1000:
            self.minSalePrice = self.resourceProduced.price - random.randrange(0,15)
        elif self.outputRes > 500:
            self.minSalePrice = self.resourceProduced.price - random.randrange(0,8)
        else:
            self.minSalePrice = self.resourceProduced.price - random.randrange(0,5)
        print "New sale price: ", self.minSalePrice
        
    def repair(self):
        #repairs equip quality
        temp = 100 - self.equipQuality
        self.equipQuality = 100
        self.money = self.money - (5*temp)

    
class TierOneBusiness(GenericBusiness): #resource harvesting business, no input. only output.
    vein = resourceClasses.RescVein(resourceClasses.Resc("tmp",-1),-1)
    claimed = False

    def __init__(self, startMon, qual, vein):
        self.money = startMon
        self.equipQuality = qual
        self.minSalePrice = vein.type.price
        self.vein = vein
        self.resourceProduced = self.vein.type

    def determineProduction(self):
        #function of quality and vein size
        self.matUse = 0
        self.produce = (self.equipQuality * 2) + (self.vein.size * 5)
        print "New production: ", self.produce

class TierTwoBusiness(GenericBusiness): #manufacturing company
    resourceInput = resourceClasses.Resc("tmp2", -2)    #TODO: add possiblity for multiple inputs
    supplier = TierOneBusiness(-1,-1,resourceClasses.RescVein(resourceClasses.Resc("tmp",-1),-1))
    hasSupplier = False
    
    def __init__(self,startMon,qual,resourceIn,resourceOut,suppList):
        self.money = startMon
        self.equipQuality = qual
        self.resourceProduced = resourceOut
        self.resourceInput = resourceIn
        self.hasSupplier = self.getSupplier(suppList)
        if self.hasSupplier == False:
            print "No supplier found, will run again at next produce cycle"
    
    def getSupplier(self,supplierList): #select supplier
        for potential in supplierList:
            if potential.vein.type == self.resourceInput and potential.produce >= self.matUse and potential.claimed == False:
                self.supplier = potential
                potential.claimed = True
                return True
            else:
                return False
            
    def produceGoods(self,suppList): #produce goods if you have supplier, get one if you don't
        if self.hasSupplier == False:
            self.hasSupplier = self.getSupplier(suppList)
            return False
        if self.inputRes >= self.matUse:
            self.outputRes += self.produce
        breakChance = random.randrange(0,10)
        if breakChance > 7:
            self.equipQuality -= 5
        self.potentialValue = (self.outputRes * self.minSalePrice) - (self.inputRes * self.resourceInput.price)
        self.inputRes -= self.inputRes
        return True
    
    
    def purchaseGoods(self): #get input from supplier
        self.money -= self.supplier.minSalePrice * self.supplier.outputRes
        self.supplier.money += self.supplier.minSalePrice * self.supplier.outputRes
        self.inputRes += self.supplier.outputRes
        self.supplier.outputRes = 0
        